// /////////////////////////////////// 
//  
//  
//     (c)Copyright 2001-2010 Xiaojun (David) Chen 
//        Authored and Owned Xiaojun (David) Chen 
//        All Right Reserved
//  
// /////////////////////////////////// 


using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using CCF20Model.ModelBase;
using WiseLink.RDS.Resource;

namespace CCF20Model
{

    public class CustomerData 
    {
        public Device MyDevice {set;get;}
        public Vehicle MyVehicle { set; get; }
        public Customer MyCustomer { set; get; }
        public bool IsReadOnly { set; get; }
        public CustomerData()
        {
         
        }

        public TransferedCustomer MyTransfered { get; set; }
    }
   
    public class SimpleCustomerInfo
    {
        public string Name { get; set; }
        public int ID { get; set; }
        public string VehicleModel { get; set; }
        public string VehicleLicense { get; set; }
    }
    public class CustomerModel : ModelBaseClass
    {
        public CustomerCollection MyCustomerData;
        public IEnumerable<VehicleType> MyVehicleTypes;
        public IEnumerable<SimpleCustomerInfo> MySimpleCustomerInfo;

        public CustomerModel(CCF20Entities ccf20Ent): base(ccf20Ent)
        {
        }
        public ObservableCollection<SimpleCustomerInfo> GetSimpleCustomerInfo()
        {
            MySimpleCustomerInfo = from c in ccf20Entities.Customers
                                        where c.Devices.Count==0
                                        orderby c.IsPreOrder descending
                                       select new SimpleCustomerInfo
                                       {
                                           Name = c.Name,
                                           ID = c.CustomerID,
                                           VehicleModel = c.Vehicles.FirstOrDefault().VehicleType.Model,
                                           VehicleLicense = c.Vehicles.FirstOrDefault().VehicleLicense
                                           
                                       };

            RefreshQuery(MySimpleCustomerInfo);
            return new ObservableCollection<SimpleCustomerInfo>(MySimpleCustomerInfo);
            
        }
        public void GetMyVehicleTypes()
        {
            if (ZICMonitorModel.IsJava)
            {
                MyVehicleTypes = from v in ccf20Entities.VehicleTypes
                                 orderby v.Manufacture,v.Model,v.EngineType
                                 select v;
            }
            else
            {
                MyVehicleTypes = from v in ccf20Entities.VehicleTypes
                                 where v.Manufacture.Equals("FAW-VW")
                                 orderby v.Manufacture, v.Model, v.EngineType
                                 select v;
            }

        }
        public CustomerCollection GetCustomer()
        {
            var query = from c in ccf20Entities.Customers
                        select new CustomerData
                        {
                            MyCustomer = c,
                            MyVehicle = c.Vehicles.FirstOrDefault(),
                            MyDevice = c.Devices.FirstOrDefault(),
                            MyTransfered = c.TransferedCustomers.FirstOrDefault(),
                            IsReadOnly = c.SN.Length>0 ? true: false
                        };
            return new CustomerCollection(query, ccf20Entities);
            
        }

        public void Save(CustomerData cd)
        {
            if (cd != null)
            {
                if (cd.MyCustomer.CustomerID == 0)
                {
                    ccf20Entities.AddToCustomers(cd.MyCustomer);
                }
                if (cd.MyVehicle.VehicleID == 0)
                {
                    ccf20Entities.AddToVehicles(cd.MyVehicle);
                    cd.MyCustomer.Vehicles.Add(cd.MyVehicle);
                }
                if (cd.MyDevice != null && cd.MyDevice.DeviceID == 0)
                {
                    ccf20Entities.AddToDevices(cd.MyDevice);
                    cd.MyCustomer.Devices.Add(cd.MyDevice);
                }
            }
            
            ccf20Entities.SaveChanges();
        
        }
        public void ConfirmedTransferCustomer(CustomerData cd,string deviceType, string terminalType,DateTime registerDate)
        {
            Device device = new Device();
            ccf20Entities.Devices.AddObject(device);
            device.IDCSimNumber = cd.MyTransfered.IDCNum;
            device.DeviceKey = cd.MyTransfered.DeviceKey;
            device.VehicleID = cd.MyVehicle.VehicleID;
            device.CustomerID = cd.MyCustomer.CustomerID;
            device.DeviceStatus = DeviceStatus.Sold;
            device.IsRegistered = true;
            device.IsManual = true;
            device.DeviceType = deviceType;
            device.TerminalType = terminalType;
            device.RegisterDate = registerDate;
            ccf20Entities.SaveChanges();
        }

        public bool IsDuplicateSN(string sn, int deviceID)
        {
            Customer customer = ccf20Entities.Customers.Where(c => c.SN.Equals(sn)).FirstOrDefault();
            if ( customer!=null )
            {
                if (customer.Devices.First().DeviceID == deviceID)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }

        }


        
    }
}
